//
//  UIColor+FWPowerful.m
//  iOSToolkit
//
//  Created by Evan Xie on 9/17/14.
//  Copyright (c) 2014 Family Wings. All rights reserved.
//

#import "UIColor+FWPowerful.h"

@implementation UIColor (FWPowerful)

+ (UIColor *)colorWithRGBHexString:(NSString *)rgbHexString alpha:(CGFloat)alpha
{
    if (!rgbHexString) {
        return [UIColor blackColor];
    }
    
    rgbHexString = [rgbHexString stringByReplacingOccurrencesOfString:@" " withString:@""];
    rgbHexString = [rgbHexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    rgbHexString = [rgbHexString stringByReplacingOccurrencesOfString:@"0x" withString:@""];
    rgbHexString = [rgbHexString stringByReplacingOccurrencesOfString:@"0X" withString:@""];
    
    unsigned int hexValue = 0;
    NSScanner *charScanner = [NSScanner scannerWithString:rgbHexString];
    [charScanner scanHexInt:&hexValue];
    
    return [UIColor colorWithRed:((hexValue >> 16) & 0xFF) / 255.0f
                           green:((hexValue >>  8) & 0xFF) / 255.0f
                            blue:(hexValue & 0xFF) / 255.0f
                           alpha:alpha];
}

+ (UIColor *)colorWithRGBHexValue:(NSUInteger)rgbHexValue alpha:(CGFloat)alpha
{
    float red = ((rgbHexValue & 0xFF0000) >> 16) / 255.0;
    float green = ((rgbHexValue & 0x00FF00) >> 8) / 255.0;
    float blue = (rgbHexValue & 0x0000FF) / 255.0;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

+ (UIColor *)colorWithRGBAHexString:(NSString *)rgbaHexString
{
    if (!rgbaHexString) {
        return [UIColor blackColor];
    }
    
    rgbaHexString = [rgbaHexString stringByReplacingOccurrencesOfString:@" " withString:@""];
    rgbaHexString = [rgbaHexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    rgbaHexString = [rgbaHexString stringByReplacingOccurrencesOfString:@"0x" withString:@""];
    rgbaHexString = [rgbaHexString stringByReplacingOccurrencesOfString:@"0X" withString:@""];
    
    unsigned int hexValue = 0;
    NSScanner *charScanner = [NSScanner scannerWithString:rgbaHexString];
    [charScanner scanHexInt:&hexValue];
    
    float red = ((hexValue >> 24) & 0xFF) / 255.0f;
    float green = ((hexValue >> 16) & 0xFF) / 255.0f;
    float blue = ((hexValue >>  8) & 0xFF) / 255.0f;
    float alpha = (hexValue & 0xFF) / 255.0;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

+ (UIColor *)colorWithRGBAHexValue:(NSUInteger)rgbaHexValue
{
    float red = ((rgbaHexValue & 0xFF000000) >> 24) / 255.0;
    float green = ((rgbaHexValue & 0x00FF0000) >> 16) / 255.0;
    float blue = ((rgbaHexValue & 0x0000FF00) >> 8) / 255.0;
    float alpha = (rgbaHexValue & 0x000000FF) / 255.0;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

@end
